﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class DepFlights : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                txtAirline.Text = Session["AIRLINE"].ToString();
                txtAirport.Text = Session["DCS_AIRPORT"].ToString();

                if (Session["FLT_DATE"] != null)
                    txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
            }
            btnChangeStatus.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnClose.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDCS_Init.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                txtFltDate.Text = "";
                return;
            }

            Session["FLT_DATE"] = txtFltDate.Text;
            GridView1.DataBind();
            //if (lstStatus.SelectedValue == "O") //Open for checking-in
            //    Panel1.Visible = true;
            //else
            //    Panel1.Visible = false;
        }

        protected void btnChangeStatus_Click(object sender, EventArgs e)
        {
            //如果从初始状态转入机场控制，要检查机尾号是否为虚拟飞机，如果是拒绝转机场控制。必须是实际机尾号，才可转机场控制。
            //转机场控制后，如果再变机尾号，ICS中只改变ICS数据，同时要通知DCS变更DCS数据。简单，但是否缺乏优越性？？
            if (Session["ACL"].ToString().IndexOf("DA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            //允许代理服务单位修改航班离港状态
            cmd.CommandText = "bll_flt_set_departure_status";
            SqlParameter para = cmd.Parameters.AddWithValue("@flt_date", strArray[0]);
            para = cmd.Parameters.AddWithValue("@op_airline", strArray[1]);
            para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[2]);
            para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());//strArray[3]
            para = cmd.Parameters.AddWithValue("@dcs_status", lstNewStatus.SelectedValue);
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        GridView1.DataBind();
                        break;
                    case 2:
                        lblError.Text = "Can not return to Initial status!";
                        break;
                    case 1:
                        lblError.Text = "Invalid flight or Flight cancelled!";
                        break;
                    case 3:
                        lblError.Text = "Flight station not found!";
                        break;
                    case 10:
                        lblError.Text = "Not first station,can not set airport control!";
                        break;
                    case 11:
                        lblError.Text = "Check last airport failed!";
                        break;
                    case 9:
                        lblError.Text = "Please set boarding gate and time first!";
                        break;
                    case 8:
                        lblError.Text = "Flown,can not set status!";
                        break;
                    case 6:
                        lblError.Text = "Invalid current status!";
                        break;
                    case 7:
                        lblError.Text = "Invalid new status!";
                        break;
                    case 4:
                        lblError.Text = "Invalid changing!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized office!";
                        break;
                    case 51:
                        lblError.Text = "Unauthorized airport!";
                        break;
                    case 55:
                        lblError.Text = "Can not reset other airline's flight status!";
                        break;
                    case 101:
                        lblError.Text = "Set dcs status failed!";
                        break;
                    case 102:
                        lblError.Text = "Set flight done failed!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        //protected void btnChkIn_Click(object sender, EventArgs e)
        //{
        //    //置机界面无需知道航班号，所以不用传参数过去
        //    string key;
        //    string[] strArray;
        //    if (Request.Form["KEY"] == null)
        //        return;
        //    key = Request.Form["KEY"];
        //    strArray = key.Split('|');

        //    Response.Redirect("check_in2.aspx?flt_no=" + strArray[2] + "&flt_date=" + strArray[0]);
        //}
        protected void btnPaxList_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            Response.Redirect("DepFltPax.aspx?flt_date=" + strArray[0] + "&airline=" + strArray[1] + "&flt_no=" + strArray[2] + "&airport=" + strArray[3]);
        }

        protected void btnSeatMap_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            //if (strArray[1] != Session["AIRLINE"].ToString())
            //{
            //    lblError.Text = "Unauthorized AIRLINE!";
            //    return;
            //}
            Response.Redirect("DepFltSeatMap.aspx?flt_date=" + strArray[0] + "&airline=" + strArray[1] + "&flt_no=" + strArray[2]);
        }
        protected void btnLogs_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            if (strArray[1] != Session["AIRLINE"].ToString())
            {
                lblError.Text = "Unauthorized AIRLINE!";
                return;
            }
            //Response.Redirect("flt_dcs_logs.aspx?flt_no=" + strArray[2] + "&flt_date=" + strArray[0]);
            Response.Redirect("FltLog.aspx?airline=" + strArray[1] + "&flt_no=" + strArray[2] + "&flt_date=" + strArray[0]);
        }


        protected void btnDCS_Init_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            if (strArray[1] != Session["AIRLINE"].ToString())
            {
                lblError.Text = "Unauthorized AIRLINE!";
                return;
            }
            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";

            cmd.CommandText = "bll_flt_initiate_for_departure";
            SqlParameter para = cmd.Parameters.AddWithValue("@flt_date", strArray[0]);
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@flt_no", strArray[2]);
            para = cmd.Parameters.AddWithValue("@user_airport", Session["DCS_AIRPORT"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Invalid flight or inproper status!";
                        break;
                    case 2:
                        lblError.Text = "Not your airline flight!";
                        break;
                    case 5:
                        lblError.Text = "Not first station,can not initiate!";
                        break;
                    case 9:
                        lblError.Text = "Invalid plane tail number!";
                        break;
                    case 8:
                        lblError.Text = "Flown,can not initiate!";
                        break;
                    case 10:
                        lblError.Text = "Flight route not found!";
                        break;
                    case 11:
                        lblError.Text = "Only the flight in next 7 days is ready to initiating!";
                        break;
                    case 100:
                        lblError.Text = "Generate aircraft instance failed!";
                        break;
                    case 101:
                        lblError.Text = "Update status failed!";
                        break;
                    case 102:
                        lblError.Text = "Update route dcs_status failed!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetBorading_Click(object sender, EventArgs e)
        {
            //必须有承运公司代理权
            if (Session["ACL"].ToString().IndexOf("DA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";

            cmd.CommandText = "bll_flt_set_bording_info";
            SqlParameter para = cmd.Parameters.AddWithValue("@flt_date", strArray[0]);
            para = cmd.Parameters.AddWithValue("@op_airline", strArray[1]);
            para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[2]);
            para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());//strArray[3]
            para = cmd.Parameters.AddWithValue("@gate", txtGate.Text);
            para = cmd.Parameters.AddWithValue("@boarding_time", txtBoardingTime.Text);
            //para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            //para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        GridView1.DataBind();
                        break;
                    case 1:
                        lblError.Text = "Updating failed!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized office!";
                        break;
                    case 51:
                        lblError.Text = "Unauthorized airport!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnFinalPax_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            //if (strArray[1] != Session["AIRLINE"].ToString())
            //{
            //    lblError.Text = "Unauthorized AIRLINE!";
            //    return;
            //}
            Response.Redirect("DepFltReport.aspx?airline=" + strArray[1] + "&flt_no=" + strArray[2] + "&flt_date=" + strArray[0]);

        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            //允许代理服务单位修改航班离港状态
            cmd.CommandText = "bll_flt_close_for_departure";
            SqlParameter para = cmd.Parameters.AddWithValue("@flt_date", strArray[0]);
            para = cmd.Parameters.AddWithValue("@op_airline", strArray[1]);
            para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[2]);
            para = cmd.Parameters.AddWithValue("@airport", Session["DCS_AIRPORT"].ToString());//strArray[3]
            para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
            para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
            try
            {
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "OK.";
                        GridView1.DataBind();
                        break;
                    case 11:
                        lblError.Text = "Check last airport failed!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized office!";
                        break;
                    case 51:
                        lblError.Text = "Unauthorized airport!";
                        break;
                    case 52:
                        lblError.Text = "Not final segment!";
                        break;
                    case 101:
                        lblError.Text = "Set flight route flown failed!";
                        break;
                    case 102:
                        lblError.Text = "Set flight flown failed!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnLoadBalance_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            //if (strArray[1] != Session["AIRLINE"].ToString())
            //{
            //    lblError.Text = "Unauthorized AIRLINE!";
            //    return;
            //}
            Response.Redirect("LoadBalance.aspx?flt_date=" + strArray[0] + "&airline=" + strArray[1] + "&flt_no=" + strArray[2]);
        }
        protected void btnMiscServices_Click(object sender, EventArgs e)
        {
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select a flight.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');
            //if (strArray[1] != Session["AIRLINE"].ToString())
            //{
            //    lblError.Text = "Unauthorized AIRLINE!";
            //    return;
            //}
            Response.Redirect("DepVA_Products.aspx?airline=" + strArray[1] + "&flt_no=" + strArray[2] + "&flt_date=" + strArray[0]);
        }
    }
}